<?php 
	// Year
	$yearList = $model->listYear();
	
	$select_year = "<select name='year' id='year'>";
    
    if( isset($_GET['year']) ){
        $year = isset($_GET['year']);
    }
    else{
        $year =  date("Y", time());
    }
    
	foreach( $yearList as $y ){
		$y = $y['tahun'];
		
		$selected = '';
		if( $y == $year ){
			$selected = "selected='selected'";
        }
			
		$select_year .= "<option value='$y' $selected>$y</option>";
	}
	$select_year .= "</select>";
    
    $select_type = "<select name='chart_type' id='chart_type'>";
    
    if( isset($_GET['chart_type']) ){
        $chart_type = isset($_GET['chart_type']);
    }
    else{
        $chart_type =  'column-column';
    }
    
    $chart_types = array(
        'line-column'   => 'Garis  (Taksasi) - Batang (Realisasi)',
        'column-line'   => 'Batang (Taksasi) - Garis  (Realisasi)',
        'line-line'     => 'Garis  (Taksasi) - Garis  (Realisasi)',
        'column-column' => 'Batang (Taksasi) - Batang (Realisasi)',
    );
    
    foreach($chart_types as $t=>$tx){
        $selected = "";
        if( $t == $chart_type ){
            $selected = "selected='selected'";
        }
        
        $select_type .= "<option value='".$t."' $selected>".$tx."</option>";
    }
    $select_type .= '</select>';
    
	$model->generateChart();
	
	$chart_detail = $model->getChartValue();
	
?>
<div class="title">Rencana VS Realisasi Panen (Betina)</div>
<form name="frm_<?=$subbagian;?>" id="frm_<?=$subbagian;?>" method="get" action="index.php?bagian=<?=$bagian;?>&subbagian=<?=$subbagian;?>&action=view" >
	<input type="hidden" name="subbagian" value="<?=$subbagian;?>" />
	<input type="hidden" name="bagian" value="<?=$bagian;?>" />
	<input type="hidden" name="action" value="view" />
	<ul>
    	<li><span>
            <label>Tahun:</label>
            <?=$select_year;?>
        </span></li>
		<li><span>
            <label>Jenis Grafik:</label>
			<?=$select_type;?>
        </span></li>
		<li class="button"><span>
			<input type="submit" value="Lihat"/>
		</span></li>
	</ul>
</form>
<script type="text/javascript" src="library/amcolumn/swfobject.js"></script>
<div id="flashcontent">
	<strong>You need to upgrade your Flash Player</strong>
</div>
<script type="text/javascript">
	// <![CDATA[		
	var so1 = new SWFObject("library/amcolumn/amcolumn.swf", "chart_main", "998", "300", "8", "#FFFFFF");
	so1.addVariable("chart_id", "chart_main");
	so1.addVariable("path", "library/amcolumn/");
	so1.addVariable("settings_file", encodeURIComponent("include/chart_setting/rencana_panen.xml"));        // you can set two or more different settings files here (separated by commas)
	so1.addVariable("data_file", encodeURIComponent("include/chart_data/rencana_panen.xml"));		
	so1.write("flashcontent");
	// ]]>
</script>

 <div id="chart_detail">
	<table>
		<tr>
			<th colspan="13">
			 Rencana Vs Realisasi Panen Keseluruhan
			</th>
		</tr>
		<tr>
			<th>Bulan</th>
			<?php 
			foreach($chart_detail as $c)
				echo '<th>'.$c[0].'</th>';
			?>
		</tr>
		<tr>
			<td>Real</td>
			<?php 
			foreach($chart_detail as $c)
				echo '<td>'.$c[1].'</td>';
			?>
		</tr>
		<tr>
			<td>Rencana</td>
			<?php 
			foreach($chart_detail as $c)
				echo '<td>'.$c[2].'</td>';
			?>
		</tr>
	</table>
 </div>
 <br/>
 <br/>
<div id="flashcontent_drill">
	<strong>You need to upgrade your Flash Player</strong>
</div>
<script type="text/javascript">
	// <![CDATA[		
	var so2 = new SWFObject("library/amcolumn/amcolumn.swf", "chart_drill_down", "998", "250", "8", "#FFFFFF");
	so2.addVariable("chart_id", "chart_drill_down");
	so2.addVariable("path", "library/amcolumn/");
	so2.addVariable("settings_file", encodeURIComponent("include/chart_setting/rencana_panen_drill.xml"));        // you can set two or more different settings files here (separated by commas)
	so2.addVariable("data_file", encodeURIComponent("include/chart_data/rencana_panen_drill.xml"));		
	so2.write("flashcontent_drill");
	// ]]>
</script>

<script language="javascript">
// CHART INITED //////////////////////////////////////////////////////////////////////////    
// amChartInited(chart_id)
// This function is called when the chart is fully loaded and initialized.
var flashMovie = new Object();

function amChartInited(chart_id){
  // get the flash object into "flashMovie" variable   
  flashMovie[chart_id] = document.getElementById(chart_id); 
}

// FIND WHICH COLUMN/BULLET WAS CLICKED //////////////////////////////////////////////////
// amClickedOnBullet(chart_id, graph_index, value, series, url, description)
// This function is called when the viewer clicks on a graph bullet. It returns the
// sequential number of a graph (graph_index), the value of the data point (value),
// the series value (series), the URL and the description attributes.
function amClickedOnBullet(chart_id, graph_index, value, series, url, description){
  // do jquery stuff
  if( chart_id == 'chart_main' && graph_index == 1 ){
	  
	  // make ajax calls
	  $.get("ajax/drillRencanaPanen.php", 
			{bulan: series, tahun: <?=isset($_GET['year']) ? $_GET['year'] : date("Y", time());?>},
			function(data){
				if( data ){
					flashMovie['chart_drill_down'].reloadAll();
					reloadTableData(data);
				}
				else{
					alert('Error occured. Drill down aborted.');
				}
			},
		   "json"  // here you specify that the return type is JSON
	  );

   }
}

// update table data
function reloadTableData(data){
	$('#chart_drill_detail').fadeOut('fast');
	for(var i=1; i<=31; i++){
		$('td.val_'+i).text(data[i]);
		console.log(data[i]);
	}		
	
	$('span.val_0').text(data[0]);
	
	// fade
	$('#chart_drill_detail').fadeIn('slow');
}

function amError(chart_id, message){
	console.log('Chart: '+chart_id+' Error');
	console.log(message);
}
</script>
 <div id="chart_drill_detail">
	<table>
		<tr>
			<th colspan="32">
			 Realisasi Panen Bulan <span class='val_0'>Tertentu</span>
			</th>
		</tr>
		<tr>
			<th>Tanggal</th>
			<?php
			for($i=1; $i<=31; $i++)
				echo '<th>'.$i.'</th>';
			?>
		</tr>
		<tr>
			<td>Real</td>
			<?php
			for($i=1; $i<=31; $i++)
				echo '<td class="val_'.$i.'"></td>';
			?>
		</tr>
	</table>
 </div>
